<?php

namespace app\admin\controller\booking;

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\AuthGroupAccess;
use app\admin\model\booking\StoreAdmin;

/**
 * 门店管理
 *
 * @icon fa fa-circle-o
 */
class Store extends Base
{

    protected $dataLimitField = 'id';
    protected $dataLimitFieldAutoFill = false;
    /**
     * Store模型对象
     * @var \app\admin\model\booking\Store
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\booking\Store;
        $this->view->assign("isDefaultList", $this->model->getIsDefaultList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    protected function getDataLimitAdminIds()
    {
        $auth = Auth::instance();
        if ($this->addonsConfig['isStoreAllocate'] == 1 && !$auth->isSuperAdmin()) {
            return $this->authStoreIds;
        }
    }

    /**
     * 门店授权
     */
    public function admin()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $list = (new Admin())
                ->field('a.id,a.username,a.nickname,sa.store_ids,GROUP_CONCAT(s.name) store_names')
                ->alias('a')
                ->join('booking_store_admin sa', 'a.id=sa.admin_id', 'LEFT')
                ->join('booking_store s', "FIND_IN_SET(s.id,sa.store_ids)", 'LEFT')
                ->order('id desc')
                ->group('a.id')
                ->select();

            $superAdminIds = AuthGroupAccess::where('group_id', 1)->column('uid');

            foreach ($list as $index => $item) {
                $item->superadmin = in_array($item['id'], $superAdminIds);
            }
            $result = array("total" => count($list), "rows" => $list);

            return json($result);
        }
        $config = get_addon_config('booking');
        $this->view->assign("isStoreAllocate", $config['isStoreAllocate']);
        return $this->view->fetch();
    }

    //更新授权
    public function update_auth()
    {
        $admin_id = $this->request->param('admin_id');
        if (!$admin_id) {
            $this->error('参数错误');
        }
        $row = (new StoreAdmin())->where('admin_id', $admin_id)->find();

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            $store_ids = $this->request->param('store_ids');
            $act = $this->request->param('act');
            if ($act == 'remove' && $row) {
                $row->store_ids = '';
                $row->save();
                $this->success('移除成功');
            }
            if (!$store_ids) {
                $this->error('参数错误');
            }
            if ($row) {
                $row->store_ids = $store_ids;
                $row->save();
            } else {
                (new StoreAdmin())->save([
                    'admin_id'  => $admin_id,
                    'store_ids' => $store_ids
                ]);
            }
            $this->success('保存成功！');
        }
        $this->assign('store_ids', !empty($row['store_ids']) ? $row['store_ids'] : '');
        return $this->view->fetch();
    }
}
